package com.ccb.jyzx.expand.tiantouonline.controller;

import com.ccb.jyzx.expand.tiantouonline.bean.DictGroup;
import com.ccb.jyzx.expand.tiantouonline.bean.bo.DictGroupBo;
import com.ccb.jyzx.expand.tiantouonline.bean.bo.PageBo;
import com.ccb.jyzx.expand.tiantouonline.bean.vo.DictGroupVo;
import com.ccb.jyzx.expand.tiantouonline.bean.vo.PageBean;
import com.ccb.jyzx.expand.tiantouonline.service.DictGroupService;
import com.ccb.jyzx.expand.tiantouonline.service.PageService;
import com.ccb.jyzx.expand.tiantouonline.view.result.Result;
import com.ccb.jyzx.expand.tiantouonline.view.result.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: extend
 * @Author: jianghaolin
 * @Version: 1.0
 */
@Controller
@RequestMapping("/tto/dict_group")
@Api(tags = "分类组操作相关接口")
public class DictGroupController extends BaseController<DictGroup> {

    @Autowired
    private DictGroupService dictGroupService;

    @Autowired
    private PageService pageService;

    @ResponseBody
    @PostMapping(value = "findAll",consumes = "application/json;charset=UTF-8")
    @ApiOperation(value = "查询所有分类", notes = "查询所有分类")
    private Result findAll(){
        List<DictGroup> list = dictGroupService.findAll();
        return ResultUtil.success(list);
    }

    @ResponseBody
    @PostMapping(value = "getPage",consumes = "application/json;charset=UTF-8")
    @ApiOperation(value = "分页查询所有分类", notes = "分页查询所有分类")
    private Result getPage(@RequestBody PageBo bo){
        if(bo == null){
            return ResultUtil.error("缺少参数");
        }
        Integer currPage = bo.getCurrPage();
        Integer pageSize = bo.getPageSize();
        int totalCount = dictGroupService.findTotleCount(bo);
        List<DictGroupVo> vos = new ArrayList<>();
        if(totalCount > 0){
            vos = dictGroupService.findByPage(bo);
        }
        PageBean pageBean = pageService.getPage(currPage, pageSize, totalCount, vos);
        return ResultUtil.success(pageBean);
    }

    @ResponseBody
    @PostMapping(value = "insert",consumes = "application/json;charset=UTF-8")
    @ApiOperation(value = "保存分类", notes = "保存分类")
    private Result insert(@RequestBody DictGroupBo pojo){
        try {
            dictGroupService.save(pojo);
            return ResultUtil.success("保存成功！");
        }catch (SQLException e){
            return ResultUtil.error(e.getMessage());
        }
    }

    @ResponseBody
    @PostMapping(value = "update",consumes = "application/json;charset=UTF-8")
    @ApiOperation(value = "更新分类", notes = "更新分类")
    private Result update(@RequestBody DictGroupBo pojo){
        try {
            dictGroupService.update(pojo);
            return ResultUtil.success("更新成功！");
        }catch (SQLException e){
            return ResultUtil.error(e.getMessage());
        }

    }

    @ResponseBody
    @PostMapping(value = "deleteMore",consumes = "application/json;charset=UTF-8")
    @ApiOperation(value = "删除分类", notes = "删除分类")
    private Result deleteMore(@RequestBody DictGroupBo bo){
        if(bo == null){
            return ResultUtil.error("未接收到参数");
        }
        String ids = bo.getIds();
        try {
            dictGroupService.deleteMore(ids);
            return ResultUtil.success("删除成功！");
        }catch (SQLException e){
            return ResultUtil.error(e.getMessage());
        }
    }

}
